package com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.action;

import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.Transaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DrawCacheManager {

    private DrawCacheManager() {

    }

    public static class InstanceHolder {
        public static DrawCacheManager instance = new DrawCacheManager();
    }

    public static DrawCacheManager getInstance() {
        return InstanceHolder.instance;
    }

    private static int currentChannel = Transaction.Channel.WHITE_DOODLE;
    private static int currentPageNum = 0;

    // <account, transactions>
    //存所有channel, 所有page的数据，channel+page做为key
    private static Map<String, Map<String, List<Transaction>>> allUserDataMaps = new HashMap<>();

    //返回的map直接用于绘制白板，不包含指令，数据格式：<account, List<Transtions>>
    public Map<String, List<Transaction>> getDrawcacheByChannelAndPage(int channel, int page) {
        String key = getDrawcacgeKey(channel, page);
        Map<String, List<Transaction>> drawCache = allUserDataMaps.get(key);
        if (drawCache == null) {
            drawCache = new HashMap<>();
            allUserDataMaps.put(key, drawCache);
        }
        return drawCache;
    }

    public Map<String, List<Transaction>> getCurrDrawcache() {
        return getDrawcacheByChannelAndPage(currentChannel, currentPageNum);
    }

    public Map<String, List<Transaction>> getDrawcacheByKey(String key) {
        Map<String, List<Transaction>> drawCache = allUserDataMaps.get(key);
        if (drawCache == null) {
            drawCache = new HashMap<>();
            allUserDataMaps.put(key, drawCache);
        }
        return drawCache;
    }

    public void putUserDataMap(String key, Map<String, List<Transaction>> userDataMap) {
        allUserDataMaps.put(key, userDataMap);
    }

    public void clearUserDataMapByKey(String key) {
        allUserDataMaps.get(key).clear();
    }

    public void revokeUserDataByKey(String key, String account) {
        Map<String, List<Transaction>> map = allUserDataMaps.get(key);

        if (map != null) {
            List<Transaction> t = map.get(account);
            for (int i = t.size() - 1; i >= 0; i--) {
                if (t.get(i).getStep() != Transaction.ActionStep.START) {
                    t.remove(i);
                    continue;
                }
                t.remove(i);
                break;
            }
            map.put(account, t);
            allUserDataMaps.put(key, map);
        }
    }

    public void syncAllUserData(Transaction t, List<Transaction> datas) {
//        clear();
        String account = t.getUid();
        for (Transaction transaction : datas) {
            String key = getDrawcacgeKey(transaction.getChannel(), transaction.getCurrentPageNum());
            Map<String, List<Transaction>> accountTrasMaps = allUserDataMaps.get(key);
            if (accountTrasMaps == null) {
                accountTrasMaps = new HashMap<>();
                List<Transaction> trans = new ArrayList<>();
                trans.add(transaction);
                accountTrasMaps.put(account, trans);
            } else {
                List<Transaction> trans = accountTrasMaps.get(account);
                if (trans == null) {
                    trans = new ArrayList<>();
                    trans.add(transaction);
                } else {
                    trans.add(transaction);
                }
                accountTrasMaps.put(account, trans);
            }
            allUserDataMaps.put(key, accountTrasMaps);
        }

    }

    public String getDrawcacgeKey(int channel, int page) {
        return "c" + channel + "p" + page;
    }

    public String getCurrKey() {
        return getDrawcacgeKey(currentChannel, currentPageNum);
    }

    public Map<String, Map<String, List<Transaction>>> getAllUserDataMaps() {
        return allUserDataMaps;
    }

    public void setAllUserDataMaps(Map<String, Map<String, List<Transaction>>> allUserDataMaps) {
        DrawCacheManager.allUserDataMaps = allUserDataMaps;
    }

    public int getCurrentChannel() {
        return currentChannel;
    }

    public void setCurrentChannel(int currentChannel) {
        DrawCacheManager.currentChannel = currentChannel;
    }

    public int getCurrentPageNum() {
        return currentPageNum;
    }

    public void setCurrentPageNum(int currentPageNum) {
        DrawCacheManager.currentPageNum = currentPageNum;
    }

    public void clear() {
        allUserDataMaps.clear();
    }
}
